package com.ruoyi.paip.util;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.zip.GZIPInputStream;

public class HttpRequestSimulation {

    public static void main(String[] args) throws IOException {
        // 请求的 URL
        //String urlString = "https://api.m.jd.com/api?functionId=pp.own.channel.biz.index.floor&appid=paipai_wx&client=m&ext=%7B%22referer%22%3A%22wx4987f0b3b115aae2%2F4.4.1%2Fpages%2Findex%2Findex%22%7D&t=1736238045618&h5st=20250107162045619%3Byyqe8vlh1ezzuee2%3Bd9bf4%3Btk03ac7df1c3f18pMyszKzMrMTVLesls4UHwq6i74xMlReQ2xz9b2XsPj4YE6KDsbATOXOvJ7wAW0BZ_bEQiu0Jj88Yt%3Bc58b258c19af1e6f2e1a50f69e7d3e28%3B4.9%3B1736238045619%3Bq3EpJjYSGWENxWVfD6FOzXVPyGEjLDIj7SFjLrJp-jZf9HIg3T0UzelNJrJdJTEjLrJp-jJX9CUZb25R4bFbJrJdJ31QHyVT5ipjLD7N%3B49318bef48b5ae775e7c475e12c736b1&uuid=17359920210391667687448&eid=93A92CCD48893AA941C77D3754ADD7A99A97F22E&body=%7B%22channel%22%3A2%7D";
        String urlString = "https://api.m.jd.com/api?" +
                "functionId=pp.own.channel.biz.index.floor" +
                "&appid=paipai_wx" +
                "&client=m" +
                "&ext=%7B%22referer%22%3A%22wx4987f0b3b115aae2%2F4.4.1%2Fpages%2Findex%2Findex%22%7D" +
                "&t=1736238045618" +
                "&h5st=20250107162045619%3Byyqe8vlh1ezzuee2%3Bd9bf4%3Btk03ac7df1c3f18pMyszKzMrMTVLesls4UHwq6i74xMlReQ2xz9b2XsPj4YE6KDsbATOXOvJ7wAW0BZ_bEQiu0Jj88Yt%3Bc58b258c19af1e6f2e1a50f69e7d3e28%3B4.9%3B1736238045619%3Bq3EpJjYSGWENxWVfD6FOzXVPyGEjLDIj7SFjLrJp-jZf9HIg3T0UzelNJrJdJTEjLrJp-jJX9CUZb25R4bFbJrJdJ31QHyVT5ipjLD7N%3B49318bef48b5ae775e7c475e12c736b1" +
                "&uuid=17359920210391667687448" +
                "&eid=93A92CCD48893AA941C77D3754ADD7A99A97F22E" +
                "&body=%7B%22channel%22%3A2%7D";

        // 创建 URL 对象
        URL url = new URL(urlString);
        
        // 打开连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法和请求头
        connection.setRequestMethod("GET");
        //connection.setRequestProperty("x-referer-page", "/pages/index/index");
        //connection.setRequestProperty("xweb_xhr", "1");
        //connection.setRequestProperty("x-rp-client", "mini_2.0.0");
        //connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x63090c11)XWEB/11581");
        //connection.setRequestProperty("x-referer-package", "wx4987f0b3b115aae2");
        //connection.setRequestProperty("accept", "*/*");
        //connection.setRequestProperty("sec-fetch-site", "cross-site");
        //connection.setRequestProperty("sec-fetch-mode", "cors");
        //connection.setRequestProperty("sec-fetch-dest", "empty");
        connection.setRequestProperty("referer", "https://servicewechat.com/wx4987f0b3b115aae2/127/page-frame.html");
        //connection.setRequestProperty("accept-encoding", "gzip, deflate, br");
        //connection.setRequestProperty("accept-language", "zh-CN,zh;q=0.9");
        //connection.setRequestProperty("cookie", "guid=5456f82547acd2a26dab66817ec201db4a9f98a3ecb04099d6c93a03e9165644; pt_pin=jd_66b89567df5ab; pt_key=AAJneSMWADDGXTuS9Ht2QLrdi1zcTHAU167PY_bLMQnd0laL5KKBQbw1F99g0IrbBVCucyavLX0; pt_token=; __jda=1.17359920210391667687448.1735992021039.1736233284.1736237242.5");

        // 获取响应代码
        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);

        // 处理响应体（如果是gzip编码的）
        InputStream inputStream;
        if ("gzip".equals(connection.getContentEncoding())) {
            inputStream = new GZIPInputStream(connection.getInputStream());
        } else {
            inputStream = connection.getInputStream();
        }

        // 读取响应数据
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder responseBody = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            responseBody.append(line);
        }

        // 打印响应体
        System.out.println("Response Body: " + responseBody.toString());

        // 关闭连接
        connection.disconnect();
    }
}
